package com.janlei.config;


import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.S3AsyncClient;
import software.amazon.awssdk.services.s3.S3Client;

import java.net.URI;
import java.net.URISyntaxException;

/**
 * s3配置
 */
@Configuration
public class S3Config {
    @Value("${aws.s3.end}")
    private String endPoint;
    @Value("${aws.s3.region}")
    private String region;
    @Value("${aws.s3.access}")
    private String accessKey;
    @Value("${aws.s3.secret}")
    private String secretKey;

    @Bean(destroyMethod = "close")
    public S3Client s3Client() throws URISyntaxException {
        URI uri = new URI(endPoint);
        return S3Client.builder()
                .region(Region.of(region))
                .endpointOverride(uri)
                .credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create(accessKey, secretKey)))
                .build();
    }


    @Bean(destroyMethod = "close")
    public S3AsyncClient s3AsyncClient() throws URISyntaxException {
        URI uri = new URI(endPoint);

        return S3AsyncClient.builder()
                .region(Region.of(region))
                .endpointOverride(uri)
                .credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create(accessKey, secretKey)))
                .build();
    }
}